package com.learning.hadoop.mapreduce.boost;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;
import java.io.StringReader;

//import org.wltea.analyzer.core.IKSegmenter;
//import org.wltea.analyzer.core.Lexeme;

/**
 * 第一个map, 计算 TF 和 N
 *
 * @author root
 */
public class FirstMapper extends Mapper<LongWritable, Text, Text, IntWritable> {

    /**
     * TF 在一个文章中出现的词频 N 总共多少文章
     * 按行传入
     */
    @Override
    protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, IntWritable>.Context context)
            throws IOException, InterruptedException {

        String[] values = value.toString().trim().split("\t");

        if (values.length >= 2) {
            String id = values[0].trim();
            String content = values[1].trim();

            // 分词(Solr)
            StringReader stringReader = new StringReader(content);
            /*IKSegmenter ikSegmenter = new IKSegmenter(stringReader, true);
            Lexeme word = null;
            while ((word = ikSegmenter.next()) != null) {
                String w = word.getLexemeText();
                context.write(new Text(w + "_" + id), new IntWritable(1));
            }*/
            context.write(new Text("count"), new IntWritable(1));
        } else {
            System.out.println(values.toString() + "---");
        }

    }

}